package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.client.RoomClient;
import com.jiyun.dto.RoomRecordDto;
import com.jiyun.entity.Room;
import com.jiyun.entity.RoomRecord;
import com.jiyun.entity.StuRoom;
import com.jiyun.pojo.MyResult;
import com.jiyun.service.IRoomRecordService;
import com.jiyun.service.IStuRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-04-18
 */
@RestController
@RequestMapping("/room-record")
public class RoomRecordController {

    @Autowired
    IRoomRecordService roomRecordService;


    @Autowired
    RoomClient roomClient;

    @Autowired
    IStuRoomService stuRoomService;
    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public MyResult addRoomRecord(@RequestBody RoomRecord roomRecord){
        /**
         * 校验操作：
         *  1、床位号是否合法---房间表的maxnum字段
         *  2、床位号是否有人---单表查询宿住宿表
         *  3、宿舍是否住满-----房间表字段对比
         * 默认数据填充
         */
        Room room = roomClient.findById(roomRecord.getDestRoomId());

        if (roomRecord.getDestNum()>room.getMaxnum()) {
            return MyResult.ERROR("床位号不合法，最大是："+room.getMaxnum());
        }
        if(room.getCurrentNum()==room.getMaxnum()){
            return MyResult.ERROR("宿舍已满员");
        }


        LambdaQueryWrapper<StuRoom> stuRoomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        stuRoomLambdaQueryWrapper.eq(StuRoom::getBid,roomRecord.getDestBid());
        stuRoomLambdaQueryWrapper.eq(StuRoom::getRid,roomRecord.getDestRoomId());
        stuRoomLambdaQueryWrapper.eq(StuRoom::getNum,roomRecord.getDestNum());

        if (stuRoomService.count(stuRoomLambdaQueryWrapper)>0) {
            return MyResult.ERROR("该床位号已经有人了");
        }


        roomRecord.setApplyTime(LocalDateTime.now());

        roomRecordService.save(roomRecord);

        return MyResult.OK();

    }

    @RequestMapping("changeState")
    public MyResult changeState(@RequestBody RoomRecord roomRecord){
        RoomRecord roomRecordDB = roomRecordService.getById(roomRecord.getId());

        if(roomRecord.getState().equals("已通过")){
            /**
             * 校验操作：
             *  1、床位号是否有人---单表查询宿住宿表
             *  2、宿舍是否住满-----房间表字段对比
             */

            Room room = roomClient.findById(roomRecordDB.getDestRoomId());

            if(room.getCurrentNum()==room.getMaxnum()){
                return MyResult.ERROR("宿舍已满员");
            }

            LambdaQueryWrapper<StuRoom> stuRoomLambdaQueryWrapper = new LambdaQueryWrapper<>();
            stuRoomLambdaQueryWrapper.eq(StuRoom::getBid,roomRecordDB.getDestBid());
            stuRoomLambdaQueryWrapper.eq(StuRoom::getRid,roomRecordDB.getDestRoomId());
            stuRoomLambdaQueryWrapper.eq(StuRoom::getNum,roomRecordDB.getDestNum());

            if (stuRoomService.count(stuRoomLambdaQueryWrapper)>0) {
                return MyResult.ERROR("该床位号已经有人了");
            }


            //修改当前住宿信息


            /**
             * 0、查询当前住宿人的住宿信息
             * 1、修改当前人的住宿信息
             * 2、当前宿舍减员
             * 3、目标宿舍增员
             */

            //* 1、修改当前人的住宿信息
            LambdaQueryWrapper<StuRoom> stuRoomLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            stuRoomLambdaQueryWrapper1.eq(StuRoom::getSid,roomRecordDB.getSid());
            StuRoom stuRoom = stuRoomService.getOne(stuRoomLambdaQueryWrapper1);//每个学生都必须有住宿信息
            stuRoom.setBid(roomRecordDB.getDestBid());
            stuRoom.setRid(roomRecordDB.getDestRoomId());
            stuRoom.setNum(roomRecordDB.getDestNum());
            stuRoomService.updateById(stuRoom);
            //* 2、当前宿舍减员
            Integer currentRid = roomRecordDB.getCurrentRid();//当前宿舍id
            Room currentRoom = roomClient.findById(currentRid);
            currentRoom.setCurrentNum(currentRoom.getCurrentNum()-1);
            roomClient.updateRoom(currentRoom);

            //* 3、目标宿舍增员
            Room destRoom = roomClient.findById(roomRecordDB.getDestRoomId());
            destRoom.setCurrentNum(destRoom.getCurrentNum()+1);
            roomClient.updateRoom(destRoom);

        }

        //修改记录
        roomRecordDB.setState(roomRecord.getState());
        roomRecordDB.setOperateTime(LocalDateTime.now());
        roomRecordService.updateById(roomRecordDB);


        return MyResult.OK();

    }
    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public MyResult updateRoomRecord(@RequestBody RoomRecord roomRecord){
        /**
         * 校验操作：
         *  1、床位号是否合法---房间表的maxnum字段
         *  2、床位号是否有人---单表查询宿住宿表
         *  3、宿舍是否住满-----房间表字段对比
         * 默认数据填充
         */
        Room room = roomClient.findById(roomRecord.getDestRoomId());

        if (roomRecord.getDestNum()>room.getMaxnum()) {
            return MyResult.ERROR("床位号不合法，最大是："+room.getMaxnum());
        }
        if(room.getCurrentNum()==room.getMaxnum()){
            return MyResult.ERROR("宿舍已满员");
        }


        LambdaQueryWrapper<StuRoom> stuRoomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        stuRoomLambdaQueryWrapper.eq(StuRoom::getBid,roomRecord.getDestBid());
        stuRoomLambdaQueryWrapper.eq(StuRoom::getRid,roomRecord.getDestRoomId());
        stuRoomLambdaQueryWrapper.eq(StuRoom::getNum,roomRecord.getDestNum());

        if (stuRoomService.count(stuRoomLambdaQueryWrapper)>0) {
            return MyResult.ERROR("该床位号已经有人了");
        }


        roomRecord.setApplyTime(LocalDateTime.now());

        roomRecordService.updateById(roomRecord);

        return MyResult.OK();
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        roomRecordService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        roomRecordService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public RoomRecord findById(@RequestParam Integer id){
        return roomRecordService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<RoomRecord> findAll(){
        return roomRecordService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public MyResult findPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody RoomRecordDto roomRecordDto){


        /**
         * 1、姓名、学号，在login库的user表
         * 2、楼栋、房间在base库的building和room表中
         * 3、调寝记录，在stu库中的room_record中
         */

        Page<RoomRecordDto> recordDtoPage = roomRecordService.findPage(page,pageSize,roomRecordDto.getXuehao());

        return MyResult.OK(recordDtoPage);


    }

}
